﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyShop.Account
{
    public partial class Login : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterHyperLink.NavigateUrl = "Register";
            //OpenAuthLogin.ReturnUrl = Request.QueryString["ReturnUrl"];

            var returnUrl = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            if (!String.IsNullOrEmpty(returnUrl))
            {
                RegisterHyperLink.NavigateUrl += "?ReturnUrl=" + returnUrl;
            }
        }

        private bool SiteSpecificAuthenticationMethod(string UserName, string Password)
        {
            var isExists = true;

            if (this.IsValid)
            {

                using (SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString))
                {
                    SqlCommand com = new SqlCommand("select * from Users where UserName = '" + UserName + "' and Password = '" + Password + "'", sqlcon);
                    DataSet dataSet = new DataSet();
                    DataTable dataTable = new DataTable();
                    SqlDataAdapter dataAdapter = new SqlDataAdapter();
                    dataAdapter.SelectCommand = com;
                    dataAdapter.Fill(dataTable);
                    dataSet.Tables.Add(dataTable);
                    if (dataTable.Rows.Count == 0) isExists = false;
                }

            }
            return isExists;

        }

        //private void OnAuthenticate1(object sender, AuthenticateEventArgs e)
        //{
        //    bool Authenticated = false;
        //    Authenticated = SiteSpecificAuthenticationMethod(Login1.UserName, Login1.Password);

        //    e.Authenticated = Authenticated;
        //}

        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            bool Authenticated = true;
            Authenticated = SiteSpecificAuthenticationMethod(Login1.UserName, Login1.Password);

            e.Authenticated = Authenticated;
        }
    }
}